package com.daniel.cms.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/4/20 17:28
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Profile({"dev", "test"})
public class SwaggerConfig {
	/**
	 * 配置Swagger的Docket的bean实例
	 *
	 * @return docket
	 */
	@Bean
	public Docket docket() {
		return new Docket(DocumentationType.SWAGGER_2)
				.groupName("Daniel")
				.apiInfo(apiInfo())
				// 设置全局参数
				// .globalOperationParameters(globalParamBuilder())
				// 设置全局响应参数
				.globalResponseMessage(RequestMethod.GET, responseBuilder())
				.globalResponseMessage(RequestMethod.POST, responseBuilder())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.daniel.cms"))
				.paths(PathSelectors.any())
				.build();
	}

	/**
	 * 配置Swagger的apiInfo
	 *
	 * @return ApiInfo
	 */
	private ApiInfo apiInfo() {
		Contact contact = new Contact("Daniel", "https://blog.csdn.net/huiqingshuiping", "1543969334@qq.com");
		return new ApiInfo(
				"CMS接口文档",
				"CMS接口文档",
				"V1.0.0",
				"https://blog.csdn.net/huiqingshuiping",
				contact,
				"Apache 2.0",
				"http://www.apache.org/licenses/LICENSE-2.0",
				new ArrayList<>());

	}


	/**
	 * 构建全局参数列表
	 *
	 * @return List
	 */
	private List<Parameter> globalParamBuilder() {
		ArrayList<Parameter> params = new ArrayList<>();
		params.add(parameterBuilder("token", "令牌", "string", "header", false).build());
		return params;
	}

	/**
	 * 创建参数
	 *
	 * @param name          名称
	 * @param desc          描述
	 * @param type          类型
	 * @param parameterType 参数类型
	 * @param required      是否必需
	 * @return ParameterBuilder
	 */
	private ParameterBuilder parameterBuilder(String name, String desc, String type, String parameterType, boolean required) {
		ParameterBuilder tokenPar = new ParameterBuilder();
		tokenPar.name(name).description(desc).modelRef(new ModelRef(type)).parameterType(parameterType).required(required).build();
		return tokenPar;
	}

	/**
	 * 创建全局响应值
	 *
	 * @return List
	 */
	private List<ResponseMessage> responseBuilder() {
		ArrayList<ResponseMessage> list = new ArrayList<>();
		list.add(new ResponseMessageBuilder().code(200).message("响应成功").build());
		list.add(new ResponseMessageBuilder().code(401).message("未经授权").build());
		list.add(new ResponseMessageBuilder().code(403).message("禁止访问").build());
		list.add(new ResponseMessageBuilder().code(404).message("找不到文件或目录").build());
		list.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").build());
		return list;
	}
}
